﻿using System;
using Guru.Web.UI;
using Col = Column<string, System.Type, int>;
using System.Collections;
using Guru.Web.Controls.Telerik;
using Guru.Data;
using System.Data;
using Telerik.Web.UI;
using Guru.Web.Controls.Asp;

public partial class UserControl_RoomFeesDetails : GuruUserControl
{
    private int _ExpandedRoomID;

    protected int RoomID
    {
        get
        {
            return int.Parse(Page.SecureQueryString["roomID"]);
        }
    }
    protected int InvoiceID
    {
        get
        {
            return int.Parse(Page.SecureQueryString["invoiceID"]);
        }
    }

    public event EventHandler CheckOut;

    protected void uxFeesGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceID", this.InvoiceID, DbType.Int32);
        DataTable dt = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Invoice_GetFeeDetails", parameters);
        uxFeesGrid.DataSource = dt;
        if (dt.Rows.Count <= 1)
        {
            uxFeesGrid.Columns.FindByUniqueName("CheckedOutAction").Visible = false;
        }
        
    }

    protected void uxFeesGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            DataRowView dataView = e.Item.DataItem as DataRowView;
            CheckBox ch = (CheckBox)e.Item.FindControl("uxCheckOut");
            if (dataView["IsCheckedOut"].IsNotNull() && dataView["IsCheckedOut"].ToString() == "1")
            {
                ch.Checked = true;
            }
        }
    }   
    protected void uxFeesGrid_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridNestedViewItem)
        {
            if (((GridNestedViewItem)e.Item).ParentItem.Expanded)
            {
                string keyValue = uxFeesGrid.Items[e.Item.RowIndex].GetDataKeyValue("RoomID").ToString();
                this._ExpandedRoomID = int.Parse(keyValue);
                GuruGrid sGrid = e.Item.FindControl("uxServicesGrid") as GuruGrid;
                sGrid.Rebind();
            }
        }
    }
    protected void uxServicesGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@RoomID", this._ExpandedRoomID, DbType.Int32);
        parameters.Add("@InvoiceID", this._ExpandedRoomID, DbType.Int32);
       
        ((GuruGrid)source).DataSource = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Invoice_GetServices", parameters);
    }

    protected void uxCheckOut_CheckedChanged(object sender, EventArgs e)
    {
        bool isLastest = true;
        foreach (GridDataItem item in this.uxFeesGrid.Items)
        {
            CheckBox ch = (CheckBox)item.FindControl("uxCheckOut");
            if (ch.Checked == false)
            {
                isLastest = false;
                break;
            }
        }
        if (isLastest)
        {
            ((HSMPage)this.Page).AlertAjaxClient(Resources.MessageManager.AlertCheckOut);
            ((CheckBox)sender).Checked = false;
            return;
        }
        else
        {
            CheckBox ch = (CheckBox)sender;
            GridDataItem item = ch.Parent.Parent as GridDataItem;
            int roomID = int.Parse(item.GetDataKeyValue("RoomID").ToString());

            GuruParameterCollection parameters = new GuruParameterCollection();
            parameters.AddUserLoggedParams();
            parameters.Add("@InvoiceID", this.InvoiceID, DbType.Int32);
            parameters.Add("@RoomId", roomID, DbType.Int32);
            parameters.Add("@Checked", ch.Checked, DbType.Boolean);
            parameters.Add("@Status", (ch.Checked) ? HSMEnums.RoomStatus.Dirty : HSMEnums.RoomStatus.SingleCheckedIn, DbType.Int16);

            HSMCommon.HSMBusiness.UpdateData("spa_HSM_Invoice_CheckOutByRoom", parameters);
            ((HSMPage)this.Page).AddAjaxResponseScript("parent.ReloadPage();");
        }
    }
}
